package com.ruoyi.web.utils.v2;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.domain.device.DeviceInfo;
import com.ruoyi.web.utils.v2.property.V2DeviceProperty;
import com.ruoyi.web.utils.v2.property.V2DevicePropertyResp;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @desc  绿色云农2.0接口远程调用
 * @author fwx 2024/1/9 9:37
 **/
@Component
public class V2RremoteCall {

    private static final String BASE_URL = "http://yun.bigdata5s.com/api";

    /**
     * @desc  根据设备信息列表,查询设备
     * @author fwx 2024/1/30 15:36
     **/
    public static List<V2DeviceInfo> getByIds(List<DeviceInfo> deviceInfos){
        if(deviceInfos.size() == 0){
            return new ArrayList<>();
        }

        String ids = deviceInfos.stream().map(DeviceInfo::getDeviceId).collect(Collectors.joining(","));
        List<V2DeviceInfo> v2Devices = V2RremoteCall.getByIds(ids);
        return v2Devices;
    }

    /**
     * @desc 根据设备id获取设备信息 - 远程
     * @author fwx 2024/1/9 9:38
     **/
    public static List<V2DeviceInfo> getByIds(String ids){

        if(ids == null){
            return new ArrayList<>();
        }

        String body = HttpRequest.get(String.format("%s/device-search/devices/getbyIds?ids=%s",BASE_URL, ids))
                .execute()
                .body();

        List<V2DeviceInfo> v2Devices = JSONArray.parseArray(body, V2DeviceInfo.class);

        return v2Devices;
    }

    /**
     * @desc  根据设备id获取设备信息 - 远程
     * @author fwx 2024/2/23 15:35
     **/
    public static V2DeviceInfo getById(String id){

        if(id == null){
            return null;
        }

        String body = HttpRequest.get(String.format("%s/device-search/devices/getbyIds?ids=%s",BASE_URL, id))
                .execute()
                .body();

        List<V2DeviceInfo> v2Devices = JSONArray.parseArray(body, V2DeviceInfo.class);

        if(v2Devices != null && v2Devices.size() > 0){
            return v2Devices.get(0);
        }

        return null;
    }

    /**
     * @desc  查询物模型属性列表 - 远程
     * @author fwx 2024/2/21 15:34
     **/
    public static List<V2FactorInfo> getFactors(){
        String body = HttpRequest.get(String.format("%s/product/metadata/getPropertyMetadataVO",BASE_URL))
                .execute()
                .body();

        List<V2FactorInfo> v2Devices = JSONArray.parseArray(body, V2FactorInfo.class);
        //过滤没有关联关系的因子
        v2Devices = v2Devices.stream().filter(factor -> StringUtils.isNotBlank(factor.getFactorKey())).collect(Collectors.toList());
        return v2Devices;
    }

    /**
     * @desc  查询设备下的因子 - 远程
     * @author fwx 2024/4/9 16:11
     **/
    public static List<V2FactorInfo> getFactorByDid(String deviceId){

        String body = HttpRequest.get(String.format("%s/product/metadata/getDevicePropertyMetadataVO?deviceId=%s",BASE_URL,deviceId))
                .execute()
                .body();

        List<V2FactorInfo> v2Devices = JSONArray.parseArray(body, V2FactorInfo.class);
        //过滤没有关联关系的因子
        v2Devices = v2Devices.stream().filter(factor -> StringUtils.isNotBlank(factor.getFactorKey())).collect(Collectors.toList());
        return v2Devices;
    }


    /**
     * @desc  根据物模型属性名字 和 产品id 查询因子key
     * @author fwx 2024/2/29 17:03
     **/
    public static String getFactorKey(String property,Long productId){
        List<V2FactorInfo> factors = getFactors();

        for (V2FactorInfo factor : factors) {
            if(Objects.equals(factor.getProductId(),productId) && Objects.equals(property,factor.getMetadataKey())){
                return factor.getFactorKey();
            }
        }

        return null;
    }

    /**
     * @desc  查询设备最新的一条数据
     * @author fwx 2024/2/26 16:04
     **/
    public static List<V2DeviceProperty> latest(String deviceId, String startTime, String endTime){
        String body = HttpRequest.get(String.format("%s/product/productDevice/%s/properties/latest?startTime=%s&endTime=%s",BASE_URL,deviceId,startTime,endTime))
                .execute()
                .body();

        String result = JSONObject.parseObject(body).getString("data");

        List<V2DevicePropertyResp> resps = JSONArray.parseArray(result, V2DevicePropertyResp.class);

        if(resps == null){
            return null;
        }

        return resps.get(0).getProperties();
    }


    /**
     * @desc  根据二维码编号获取设备id
     * @author fwx 2024/1/9 10:55
     **/
    public static String getDeviceId(String qrCode){

        String deviceId = null;

        String body = HttpRequest.get(String.format("%s/product/productDevice/qrCode?code=%s",BASE_URL, qrCode))
                .execute()
                .body();

        JSONObject result = JSONObject.parseObject(body).getJSONObject("data");

        if(result != null){
            deviceId = result.getString("id");
        }

        return deviceId;
    }

    public static void main(String[] args) {
        List<V2DeviceProperty> latest = latest("27005000130000384753524E", "2024-02-19+00:00:00", "2024-02-26+23:59:59");
    }

}
